package com.zoewin.zephyr.reportjasper.factory.exporter;

import jcifs.util.Base64;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRRtfExporter;
import org.springframework.ui.jasperreports.JasperReportsUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * Created by WQL on 2019/12/18
 * CEASE TO STRUGGLE AND YOU CEASE TO LIVE
 */
public class WordExporter implements IExporter {

    @Override
    public String getBase64(Object jasperPrint) {
        byte[] bytes = new byte[0];
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        JRRtfExporter docReport = new JRRtfExporter();
        try {
            JasperReportsUtils.render(docReport,(JasperPrint)jasperPrint,baos);
        } catch (JRException e) {
            e.printStackTrace();
        }
        bytes = baos.toByteArray();
        String base64 = Base64.encode(bytes);
        return base64;
    }

    @Override
    public void getOutPutStream(Object jasperPrint) throws Exception {
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        OutputStream os = null;
        response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        try {
            os = response.getOutputStream();
            try {
                JRRtfExporter docReport = new JRRtfExporter();
                JasperReportsUtils.render(docReport,(JasperPrint)jasperPrint,os);
            } catch (JRException e) {
                e.printStackTrace();
                throw new Exception("输出数据流失败, 失败原因: "+ e.toString());
            }
        } catch (IOException e) {
            throw new Exception("获取流对象失败, 失败原因: "+ e.toString());
        }finally {
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                }
            }
        }
    }

    @Override
    public void getDownLoad(Object jasperPrint, String downLoadPath) throws Exception {
        JRRtfExporter docReport = new JRRtfExporter();
        docReport.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,downLoadPath);
        docReport.setParameter(JRExporterParameter.JASPER_PRINT, (JasperPrint)jasperPrint);
        try {
            docReport.exportReport();
        } catch (JRException e) {
            e.printStackTrace();
            throw new Exception("获取下载pdf失败, 失败原因: "+ e.toString());
        }
    }

    @Override
    public byte[] getByteArr(Object jasperPrint) {
        byte[] bytes = new byte[0];
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        JRRtfExporter docReport = new JRRtfExporter();
        try {
            JasperReportsUtils.render(docReport,(JasperPrint)jasperPrint,baos);
        } catch (JRException e) {
            e.printStackTrace();
        }
        bytes = baos.toByteArray();
        return bytes;
    }
}
